﻿using System;
using System.Collections.Generic;
using System.Linq;
using Velocity4Net.Ctx;

namespace Velocity4Net.App.Events
{
   class MethodExceptionExecutor : EventHandlerMethodExecutor
    {
        private IContext context;
        private Type claz;
        private String method;
        private System.Exception e;
        
        private Object result;
        private bool executed = false;
    
       public MethodExceptionExecutor(
                IContext context,
                Type claz,
                String method,
                System.Exception e)
        {
            this.context = context;
            this.claz = claz;
            this.method = method;
            this.e = e;
        }

        /**
         * Call the method methodException()
         *  
         * @param handler call the appropriate method on this handler
         * @exception Exception generic exception thrown by methodException event handler method call
         */
        public void execute(EventHandler handler)
        {
            MethodExceptionEventHandler eh = (MethodExceptionEventHandler) handler;
            
            //if (eh is ContextAware)
            //    ((ContextAware) eh).setContext(context);

            executed = true;
            result = ((MethodExceptionEventHandler) handler).methodException(claz, method, e);
        }

        public Object getReturnValue()
        {
            return result;
        }

        /**
         * Only run the first MethodExceptionEventHandler
         * 
         * @return true after this is executed once.
         */
        public bool isDone()
        {
           return executed;
        }        
        
        
    }
}
